<template>
  <div>
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="学科名">
        <el-input
          v-model="queryParams.sname"
          placeholder="请输入学科名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
      <el-button
        type="danger"
        @click.prevent="doBatchDelete()"
        style="margin-left: 660px"
        ><i class="el-icon-delete-solid"></i>批量删除</el-button
      >
      <el-button type="success" @click.prevent="handleAdd()"
        ><i class="el-icon-circle-plus-outline"></i>新增</el-button
      >
    </el-form>

    <el-table
      :data="subjectList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="学科编号" prop="id" width="500">
      </el-table-column>
      <el-table-column prop="sname" label="学科名称" width="500">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="row">
      <div class="col">个数:{{ count }} 页数:{{ page }}/{{ pageCount }}</div>
      <div class="col">
        <nav aria-label="学科分页">
          <ul class="pagination">
            <li class="page-item">
              <a class="page-link" href="#" @click="firstPage()">首页</a>
            </li>
            <li class="page-item">
              <a class="page-link" href="#" @click="previousPage()">上页</a>
            </li>
            <li class="page-item">
              <a class="page-link" href="#" @click="nextPage()">下页</a>
            </li>
            <li class="page-item">
              <a class="page-link" href="#" @click="lastPage()">末页</a>
            </li>
          </ul>
        </nav>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "SubjectList",
  data() {
    return {
      subjectList: [],
      id: 0,
      page: 1,
      count: 0,
      pageCount: 0,
      rows: 5,
      multipleSelection: [],
      queryParams: { pageNum: 1, pageSize: 6, id: null, sname: null },
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getList() {
      this.axiosJSON
        .post("/subject/getAll", this.queryParams)
        .then((result) => {
          this.subjectList = result.data.data.list.list;
          this.queryParams = result.data.data.params;
          this.count = result.data.data.list.total;
          this.pageCount = result.data.data.list.pages;
        });
    },
    selectSubject(id) {
      //点击TR事件处理
      this.id = id;
      this.$parent.id = id; //辅助给父组件的数据属性
    },
    checkSelected(id) {
      //检查id是否被选中
      return this.id == id;
    },
    nextPage() {
      if (this.page < this.pageCount) {
        this.page++;
        this.getList();
      }
    },
    previousPage() {
      if (this.page > 1) {
        this.page--;
        this.getList();
      }
    },
    firstPage() {
      this.page = 1;
      this.getList();
    },
    lastPage() {
      this.page = this.pageCount;
      this.getList();
    },
    doBatchDelete() {
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let selections = "";
        for (let i = 0; i < this.multipleSelection.length; i++) {
          selections += this.multipleSelection[i].id + ",";
        }
        selections = selections.substr(0, selections.lastIndexOf(","));
        this.axiosJSON
          .post("/subject/batchDel?selections=" + selections)
          .then((result) => {
            if (result.data.code == 0) {
              this.$message({ type: "success", message: result.data.msg });
              this.getList();
            } else {
              this.$message.error(result.data.msg);
            }
          });
      });
    },
    // 查看详情
    handleView(row) {
      this.$router.push("/subject/view/" + row.id);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axiosJSON.get("/subject/delete?id=" + row.id).then((result) => {
            if (result.data.code == 0) {
              this.$message({
                type: "success",
                message: result.data.msg,
              });
              //如何刷新子组件的数据,使用事件发送机制
              this.oaevent.$emit("updateSubjectList");
            } else {
              this.$message.error(result.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.$router.push("/subject/add");
    },
  },
  mounted() {
    this.$parent.subjectTitle = "学科管理";
    this.$parent.id = 0;
    this.oaevent.$on("updateSubjectList", () => {
      this.getList();
    }); //响应updateSubjectList事件
    this.getList();
  },
};
</script>

<style>
</style>
